<?php

namespace App\Models\User;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Model;

class UserOptionsGongchangModel extends Model
{
    protected $table = 'users_options_gongchang';


    /**
     *  获取用户公司更多信息
     */
    public function getgongchangkuozhanxinxi($uid)
    {
        $model = $this->where('uid', $uid)->first();
        if (!$model) {
            return null;
        }
         if ($model && $model->legal_licences) {
            $model->legals =Fun::objToArr( $model->legal_licences);
        }
        if ($model && $model->com_licences) {
            $model->coms = Fun::objToArr($model->com_licences);
        }
        return $model->toArray();
    }

    /**
     * 设置用户公司更多信息
     */
    public function setgognchangkuozhanxinxi($uid, Array $updata)
    {
    // print_r($uid);exit;
        $count = self::where([
            'uid' => $uid
        ])->update($updata);
        if ($count !==false) {
            return true;
        } else {
            $new = new self;
            foreach ($updata as $key => $vo) {
                $new->{$key} = $vo;
            }
            $new->uid = $uid;
            $new->created_at = time();
            return $new->save();
        }
    }


    /**
     * 公司产品信息
    */
    public function getOptionProList($uid){
        $model = $this
            ->where('uid',$uid)
            ->first();
        if ($model && $model->products) {
            $pros = Fun::objToArr($model->products);
            return array(
                'zhens' => $pros['zhens'],
                'suos' => $pros['suos'],
                'baos' => $pros['baos'],
            );
        }
        return array(
            'zhens' => array(),
            'suos' => array(),
            'baos' => array(),
        );
    }


      /**
     * 工厂设备，获取
     */
    public function getOptionEquipList($uid)
    {

        $model = $this
            ->where('uid',$uid)
            ->first();
        if ($model && $model->equips) {
            return Fun::objToArr($model->equips);
        }
    }

    /**
     * 企业产品，设置
     */
    public function setOptionProList($uid,$data)
    {
        $model = $this
            ->where('uid',$uid)
            ->first();
        if (!$model) {
            $dataArr = array(
                'uid' => $uid,
                'products' => json_encode($data),
                'created_at' => time()
            );
            return $this->insertGetId($dataArr);
        } else {
            return $this->where('uid',$uid)
                ->update(array(
                    'products' => json_encode($data),
                ));
        }
    }



    /**
     * 公司风格，获取
     */
    public function getOptionStyleList($uid)
    {
        $model = $this
            ->where('uid',$uid)
            ->first();
        if ($model && $model->styles) {
            return explode(",",$model->styles);
        }
        return array();
    }


     /**
     * 企公司风格，设置
     */
    public function setOptionStyleList($uid,$data)
    {
        //var_dump($data);exit;
        $model = $this
            ->where('uid',$uid)
            ->first();
        if (!$model) {
            $dataArr = array(
                'uid' => $uid,
                'styles' => $data,
                'created_at' => time(),
            );
            return $this->insertGetId($dataArr);
        } else {
            return $this->where('uid',$uid)
                ->update(array(
                    'styles' => $data,
                ));
        }
    }


      /**
     * 产品种类，获取
     */
    public function getOptionProTypesList($uid)
    {
        $model = $this->where('uid',$uid)->first();
        if ($model && $model->pro_types) {
            return Fun::objToArr($model->pro_types);
        }
        return array();
    }

     /**
     * 产品种类，设置
     */
    public function setOptionProTypesList($uid,$data)
    {
        $model = $this->where('uid',$uid)->first();
        if($data){
            $json=json_encode($data);
        }else{
            $json='';
        }
       // var_dump($json);exit;
        if (!$model) {
            $dataArr = array(
                'uid' => $uid,
                'pro_types' => $json,
                'created_at' => time()
            );
            return $this->insertGetId($dataArr);
        } else {
            return $this->where('uid',$uid)
                ->update(array(
                    'pro_types' => $json,
                ));
        }
    }



     /**
     * 款式，获取
     */
    public function getOptionDesignList($uid)
    {
        $model = $this->where('uid',$uid)->first();
        if ($model && $model->designs) {
            return Fun::objToArr($model->designs);
        }
        return array();
    }

     /**
     * 款式，设置
     */
    public function setOptionDesignList($uid,$data)
    {
        $model = $this->where('uid',$uid)->first();
        if (!$model) {
            $dataArr = array(
                'uid' => $uid,
                'designs' => json_encode($data),
                'created_at' => time()
            );
            return $this->insertGetId($dataArr);
        } else {
            return $this->where('uid',$uid)////->where('type',$tye)
                ->update(array(
                    'designs' => json_encode($data),
                ));
        }
    }



    /**
     * 产量产能，获取
     */
    public function getOptionYield($uid)
    {
      //  var_dump($type);exit;
        $model = $this->where('uid',$uid)->first();
        if ($model && $model->yields) {
            return json_decode($model->yields);
        }
        $dataArr = array(
            'departs' => array(
                array('key'=>'生产线','num'=>0),
                array('key'=>'检验','num'=>0),
                array('key'=>'全厂总人数','num'=>0),
            ),
            'dayOutPuts' => array(0,0),
            'requirePros' => array(
                array('key'=>'市场货','val'=>0),
                array('key'=>'半精品','val'=>0),
                array('key'=>'全精品','val'=>0),
                array('key'=>'外销','val'=>0),
            ),
            'requireDeparts' => array(
                array('key'=>'组检','num'=>0),
                array('key'=>'尾查','num'=>0),
                array('key'=>'巡检','num'=>0),
            ),
            'standards' => array(0,0,0,0,0),
        );
        return $dataArr;
    }

    /**
     * 产量产能，设置
     */
    public function setOptionYield($uid,$data)
    {
        $model = $this->where('uid',$uid)->first();
        if (!$model) {
            $dataArr = array(
                'uid' => $uid,
                'yields' => json_encode($data),
                'created_at' => time(),
            );
            return $this->insertGetId($dataArr);
        } else {
            return $this->where('uid',$uid)///->where('type',$type)
                ->update(array(
                    'yields' => json_encode($data),
                ));
        }
    }



      /**
     * 环境，获取
     */
    public function getFacEnvList($uid)
    {
        $model = $this->where('uid',$uid)->first();
        if (!$model || !$model->envs) { return array(); }
        return Fun::objToArr($model->envs);
    }


     /**
     * 更新数据,如果不存在,则新加
    */
    public function upDataJson(Array $updata){

        $count=$this->where([
            'uid'=>OA::$user->getId()
        ])
           // ->where('type',$type)
            ->update($updata);
        if($count==1){
            return true;
        }else{
            $new=new self;
            foreach ($updata as $key=>$vo){
                $new->{$key}=$vo;
            }
            $new->uid=OA::$user->getId();
            $new->created_at=time();
            return $new->save();
        }
    }



      /**
     * 工厂设备，设置
     */
    public function setOptionEquip($uid,$data)
    {
        $model = $this->where('uid',$uid)->first();
        if (!$model) {
            $dataArr = array(
                'uid' => $uid,
                'equips' => json_encode($data),
                'created_at' => time(),
            );
            return $this->insertGetId($dataArr);
        } else {
            return $this->where('uid',$uid)
                ->update(array(
                    'equips' => json_encode($data),
                ));
        }
    }

}